package com.kbbywyclient.presenter;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.VerifyPhoneContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.OkHttpUtils;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/14 17:30.
 */

public class VerifyPhonePresenter extends BasePresenter implements VerifyPhoneContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    private VerifyPhoneContract.View view;

    @Inject
    public VerifyPhonePresenter(VerifyPhoneContract.View view){
        this.view=view;
    }

    @Override
    public void sendSms(String phoneNumber, String type) {
        view.showLoadingDialog(mContext.getString(R.string.loading));
        Map<String, Object> params = new HashMap<>();
        params.put("mobile", phoneNumber);
        params.put("Type", type);
        ApiServiceHelper.
                appointThreadNormal(OkHttpUtils.postData(API.SMS, params)).
                subscribe(new Subscriber<JSONObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(JSONObject jsonObject) {
                        view.closeLoadingDialog();
                        if ("1".equals(jsonObject.getString("status"))) {
                            //发送成功
                            view.onSmsSuccess(jsonObject.getString("sessionID"));
                        } else {
                            //发送失败
                            ToastUtil.showShort(jsonObject.getString("result"));
                            view.onSmsFailure();
                        }
                    }
                });
    }

    @Override
    public void bindCard(HashMap<String, String> strParams, HashMap<String, Integer> intParams) {
        view.showLoadingDialog(mContext.getString(R.string.loading));
        for(Map.Entry<String,String> entry:strParams.entrySet()){
            jsonUtil.put(entry.getKey(),entry.getValue());
        }
        for(Map.Entry<String,Integer> entry:intParams.entrySet()){
            jsonUtil.put(entry.getKey(),entry.getValue());
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.bindBankCard(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<BaseModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(BaseModel model) {
                        view.closeLoadingDialog();
                        view.onBindCardSuccess(model);
                    }
                });

    }
}
